package zhoufu.sz.sanxiao_check.ui.adapter.items;


import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import eu.davidea.flexibleadapter.FlexibleAdapter;
import eu.davidea.flexibleadapter.common.DividerItemDecoration;
import eu.davidea.flexibleadapter.common.SmoothScrollGridLayoutManager;
import eu.davidea.flexibleadapter.items.AbstractFlexibleItem;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.databinding.ItemFireChildBinding;
import zhoufu.sz.sanxiao_check.models.DangerModel;
import zhoufu.sz.sanxiao_check.ui.adapter.FireSubItemAdapter;

/**
 * 项目 ： XTSanXiao_V2
 * 开发 ： hyc
 * 时间 ： 16-8-4 上午11:23
 * 功能 ： 展示消防检查页面
 * 持有两个分别展示 整改前后图片的 recyclerview
 */
public class FireSafeChildItem extends ExpandableChildItem<DangerModel> {

    private Context _context;
    private FlexibleAdapter _flexibleAdapter;
    private int _position;

    @Override
    public boolean equals(Object o) {
        if (o instanceof FireSafeChildItem) {
            FireSafeChildItem item = (FireSafeChildItem) o;
            return item.getDB().getID() == this.getDB().getID();
        }
        return false;
    }

    @Override
    public void setDB(DangerModel name) {
        super.setDB(name);
    }

    @Override
    protected void setVariable(FlexibleAdapter adapter, ViewHolder holder, int position, List payloads) {
        super.setVariable(adapter, holder, position, payloads);
        ItemFireChildBinding childBinding = (ItemFireChildBinding) holder._dataBinding;
        _flexibleAdapter = adapter;
        _context = adapter.getContext();
        childBinding.getRoot().setTag(position);
        _position = position;
        RecyclerView before = childBinding.rvChildBefore;
        RecyclerView after = childBinding.rvChildAfter;
        /**
         *
         * 将position 存入tag,便于修改数据时 指定正确的位置数据
         *
         */
        before.setTag(position);
        after.setTag(position);

        List<DangerModel.BeforeImgsEntity> before_imgs = getDB().getBefore_Imgs();
        List<DangerModel.BeforeImgsEntity> applying_imgs = getDB().getApplying_Imgs();

        initImgRecyclerview(before, before_imgs, position);
        initImgRecyclerview(after, applying_imgs, position);

    }

    public int getPosition() {
        return _position;
    }

    public static boolean isBeforeRV(RecyclerView rv) {
        return rv.getId() == R.id.rv_child_before;
    }

    private void initImgRecyclerview(RecyclerView recyclerView, List<DangerModel.BeforeImgsEntity> imgs, int position) {

        ShowPicsItem item;
        ArrayList<ShowPicsItem> items = new ArrayList<>();

        if (imgs == null) {
            imgs = new ArrayList<>();
        }

        FireSubItemAdapter adapter = (FireSubItemAdapter) recyclerView.getAdapter();
        if (adapter == null) {
            imgs.add(new DangerModel.BeforeImgsEntity());
            if (isBeforeRV(recyclerView)) {
                getDB().setBefore_Imgs(imgs);
            } else {
                getDB().setApplying_Imgs(imgs);
            }
        }


        List<DangerModel.BeforeImgsEntity> imgEntities = imgs;

        if (null == imgEntities || imgEntities.size() == 0) {
            item = new ShowPicsItem();
            items.add(item);
        } else {
            for (DangerModel.BeforeImgsEntity entity : imgEntities) {
                item = new ShowPicsItem();
                item.setDB(entity);
                items.add(item);
            }
        }

        ArrayList<AbstractFlexibleItem> list = new ArrayList(items);


        // FIXMEED: 16-8-4  recyclerview 嵌套 导致 复用
        //== 不能通过阻止复用recyclerview,只能通过修改数据来更新adapter实现.

        if (adapter == null) {
            GridLayoutManager gridLayoutManager = new SmoothScrollGridLayoutManager(_context, 3);
            recyclerView.setLayoutManager(gridLayoutManager);
            recyclerView.addItemDecoration(new DividerItemDecoration(_context, R.drawable.default_divide));
            adapter = new FireSubItemAdapter(this, list, _flexibleAdapter.mItemClickListener);
            recyclerView.setAdapter(adapter);
        } else {
            adapter.updateDataSet(list);
            //adapter.notifyDataSetChanged();
        }
    }

    @Override
    public int getLayoutRes() {
        return R.layout.item_fire_child;
    }
}
